
package com.be.beadmin.design.impl;

import cn.hutool.core.util.ObjectUtil;
import com.be.beadmin.common.entity.PageEntity;
import com.be.beadmin.common.exception.BeadminException;
import com.be.beadmin.common.utils.JpaQueryBuilder;
import com.be.beadmin.common.utils.JpaQueryBuilderNew;
import com.be.beadmin.common.utils.JpaSortBuilder;
import com.be.beadmin.design.dto.input.ProcessDto;
import com.be.beadmin.design.dto.output.ProcessEx;
import com.be.beadmin.design.entity.BopInfo;
import com.be.beadmin.design.entity.Process;
import com.be.beadmin.design.entity.Process_;
import com.be.beadmin.design.entity.StandProductProcessInfo;
import com.be.beadmin.design.mapper.ProcessMapper;
import com.be.beadmin.design.mapper.ProcessMapperEx;
import com.be.beadmin.design.repository.BopInfoRepository;
import com.be.beadmin.design.repository.ProcessRepository;
import com.be.beadmin.design.repository.StandProductProcessInfoRepository;
import com.be.beadmin.design.service.ProcessService;
import com.be.beadmin.service.service.SerialNumberService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.criteria.Order;
import javax.persistence.criteria.Predicate;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
* @description 服务实现
* @author Jiahao Liu
* @date 2023-02-19
**/
@Service
@RequiredArgsConstructor
public class ProcessServiceImpl implements ProcessService {

    @Autowired
    private  ProcessRepository processRepository;
    @Autowired
    private  ProcessMapper processMapper;
    @Autowired
    private  ProcessMapperEx processExMapper;

    @Autowired
    private SerialNumberService serialNumberService;

    @Autowired
    private BopInfoRepository bopInfoRepository;

    @Autowired
    private StandProductProcessInfoRepository standProductProcessInfoRepository;

    @Override
    public Page<ProcessEx> getProcessByPage(PageEntity pageEntity) {
            Pageable pageable = PageRequest.of(
            pageEntity.getCurrent() -1,
            pageEntity.getPageSize());
            Page<Process> page = processRepository.findAll((root, criteriaQuery, criteriaBuilder) -> {
                    List<Predicate> list = new ArrayList<>();
                    Predicate pre1 =   JpaQueryBuilderNew.getPredicate(root, pageEntity.getFilters(), criteriaBuilder);
                    list.add(pre1);
                    Predicate pre2 = criteriaBuilder.equal(root.get(Process_.delete), 0); //过滤为0
                    list.add(pre2);
                    Predicate[] p = new Predicate[list.size()];
                    List<Order> orders = new ArrayList<>();
                    if(pageEntity.getSorters() != null && pageEntity.getSorters().size() > 0) {
                        orders = JpaSortBuilder.getPredicate(root, pageEntity.getSorters(), criteriaBuilder);
                        criteriaQuery.orderBy(orders);
                    } else {
                        criteriaQuery.orderBy(criteriaBuilder.desc(root.get(Process_.createTime).as(Date.class)));
                    }
                    return  criteriaBuilder.and(list.toArray(p));
                    }
                    , pageable);
            Page<ProcessEx> page1 = page.map(processExMapper::toEntity);
            return page1;

    }

    @Override
    public void addProcess(ProcessDto processDto) {
            Process result = processRepository.findProcessByIdAndDelete(processDto.getId(), 0);
            if(ObjectUtil.isNotEmpty(result)) {
                throw new BeadminException("Process不存在,请重新查询");
            }
            Process result2 = processRepository.findProcessByProcessNameAndDelete(processDto.getProcessName(),0);
            if (result2!=null) {
                throw new BeadminException(processDto.getProcessName()+"工序已存在");
            }

            String processCode = serialNumberService.getNextSnByCode("ProcessCode");
            processDto.setProcessCode(processCode);
            Process process = processMapper.toEntity(processDto);
            processRepository.save(process);
    }

    @Transactional
    @Override
    public void updateProcess(ProcessDto processDto) {
            Process process = processRepository.findProcessById(processDto.getId());
            int pre_f = process.getIsProductionProcess();
            int post_f = processDto.getIsProductionProcess();

            int pre_showSelfInspection = process.getShowSelfInspection();
            int post_showSelfInspection = processDto.getIsProductionProcess();

            if(pre_f!=post_f||pre_showSelfInspection!=post_showSelfInspection){
                //是否属于生产工序，修改了
                //修改目前所有的涉及该工序的工艺路径
                List<BopInfo> bopInfoList = bopInfoRepository.findBopInfoByProcessIdAndDelete(process.getId(),0);
                for(BopInfo bopInfo:bopInfoList){
                    bopInfo.setIsProductionProcess(post_f);
                    bopInfo.setShowSelfInspection(post_showSelfInspection);
                }
                bopInfoRepository.saveAll(bopInfoList);

                //修改目前所有的涉及该工序的标准柜体工序
                List<StandProductProcessInfo> standProductProcessInfoList = standProductProcessInfoRepository.findStandProductProcessInfoByProcessIdAndDelete(process.getId(),0);
                for(StandProductProcessInfo standProductProcessInfo:standProductProcessInfoList){
                    standProductProcessInfo.setIsProductionProcess(post_f);
                    standProductProcessInfo.setShowSelfInspection(post_showSelfInspection);
                }
                standProductProcessInfoRepository.saveAll(standProductProcessInfoList);
            }

            processRepository.save(processMapper.toEntity(processDto));
    }


    @Override
    public void delProcess(String id) {
        Process process = processRepository.findProcessById(id);
        process.setDelete(1);
        processRepository.save(process);
    }

    @Override
    public void delProcesss (List<String> ids) { }

    @Override
    public List<ProcessDto> getAllProcess() {
        List<Process> processList = processRepository.findAllByDelete(0);
        return processMapper.toDto(processList);
    }

    @Override
    public Process getProcessById(String processId) {
        Process process = processRepository.findProcessById(processId);
        return process;
    }


}